package com.test.burnincash.aaahpoppomelo.util.http_hpoppomelo;

import android.text.TextUtils;

import com.test.burnincash.aaahpoppomelo.activity.MainActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.UserData_hpoppomelo;
import com.test.burnincash.bean_burnin.config_info_burnin.UrlInfo_burnin;
import com.test.burnincash.dialog_burnin.LoadingDialog_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;

import org.json.JSONArray;
import org.json.JSONObject;

public class HttpPresenter_hpoppomelo {

    private static class Inner {
        private static final HttpPresenter_hpoppomelo instance = new HttpPresenter_hpoppomelo();
    }

    private HttpPresenter_hpoppomelo() {}

    public static HttpPresenter_hpoppomelo getInstance() {
        return Inner.instance;
    }

    private BaseActivity_hpoppomelo activity;

    public LoadingDialog_burnin progressBarDialog;

    public void setActivity(BaseActivity_hpoppomelo activity) {
        if (progressBarDialog != null && progressBarDialog.isShowing()) {
            progressBarDialog.dismiss();
        }
        if (activity != null) progressBarDialog = new LoadingDialog_burnin(activity);
        this.activity = activity;
    }

    public void progressBarDialogShow() {
        if (progressBarDialog == null) return;
        progressBarDialog.show();
    }

    public void progressBarDialogDismiss() {
        if (progressBarDialog == null) return;
        progressBarDialog.dismiss();
    }

    public void showToastOnUiThread(String text) {
        if (activity != null) {
            activity.showToastOnUiThread_burnin(text);
        }
    }

    //通知Activity处理响应
    public void notifyActivityHandleResponse(String word, JSONObject responseJson) {
        if (activity == null) return;
        try {
            String gcode = responseJson.optString("code");
            String gmsg = StringUtil_burnin.getSafeString(responseJson.optString("msg"));
            Object gobj = responseJson.opt("obj");

            //失败响应
            if (StringUtil_burnin.safeParseInt(gcode)!=0) {
                onResponseFail(word,gmsg);
                return;
            }

            //成功响应
            onResponseSuccess(word,gmsg,gobj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //成功响应
    private void onResponseSuccess(String word,String gmsg,Object gobj){
        if (gobj instanceof JSONObject) {
            JSONObject objJsonObject = (JSONObject) gobj;
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getXc2())){
                if (activity instanceof MainActivity_hpoppomelo) ((MainActivity_hpoppomelo)activity).onDataUploadSuccess();
                return;
            }
        } else if (gobj instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) gobj;
            if (word.equalsIgnoreCase(UrlInfo_burnin.getInstance().getNvb())){
                if (jsonArray.length()>0){
                    UserData_hpoppomelo.parse(jsonArray.optJSONObject(jsonArray.length()-1).optString("type"));
                    if (activity instanceof MainActivity_hpoppomelo) ((MainActivity_hpoppomelo)activity).onDataGet();
                }
                return;
            }
        } else {
            //其它类型
            gmsg = StringUtil_burnin.getSafeString(gmsg);
            if (TextUtils.isEmpty(gmsg)) {
                gmsg = "Response something error";
            }
            activity.showToast_burnin(gmsg);
        }
    }

    //失败响应
    private void onResponseFail(String word,String gmsg){
        activity.showToast_burnin(gmsg);
        switch (word){

        }
    }

}
